package com.lss.c_slidingWindow;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lss
 * @description
 * @createDate 2024/12/1-21:12
 */
public class lc_03 {
    public static void main(String[] args) {

    }

    /**
     * 不含有重复字符的 最长子串的长度。
     *
     * @param s
     * @return
     */
    public int lengthOfLongestSubstring(String s) {
        int len = s.length();
        Map<Character, Integer> map = new HashMap<>();
        int ans = 0;
        int left = 0;
        // 还是得注意边界问题
        for (int i = left; i < len; i++) {
            if (map.containsKey(s.charAt(i))) {
                // 仍然是边界问题
                left = Math.max(left, map.get(s.charAt(i)) + 1);
            }
            map.put(s.charAt(i), i);
            ans = Math.max(ans, (i - left + 1));
        }

        return ans;
    }


    public int lengthOfLongestSubstring_01(String s) {
        int ans = 0;
        int left = 0;
        Boolean[] has = new Boolean[26];
        char[] chars = s.toCharArray();

        for (int i = 0; i < chars.length; i++) {
            char aChar = chars[i];
            while (has[aChar]) {
                has[chars[left]] = false;
                left++;
            }
            has[aChar] = true;
            ans = Math.max(ans, i - left + 1);
        }

        return ans;
    }
}
